package v1

import (
	"context"
	"wxpay/internal/dto"
	"wxpay/internal/service"
	wxpay "wxpay/pkg/api/v1"
)

// 提交转账（幂等，可重入）POST /api/v1/wxpay/transfer/apply
func (s *Server) TransferApply(ctx context.Context, req *wxpay.TransferApplyRequest) (*wxpay.TransferApplyResponse, error) {
	transferDto := dto.TransferDetailDTO{
		BizDetailNo: req.BizNo,
		Amount:      req.Amount,
		Remark:      req.Remark,
		OpenID:      req.OpenId,
		UserName:    req.UserName,
		UserIDCard:  req.UserIdCard,
		AppID:       req.AppId,
		MchID:       req.MchId,
	}
	returnDTO, err := service.NewTransferService(s.d, s.redis).CreateTransferDetail(ctx, &transferDto)
	if err != nil {
		return nil, err
	}
	return &wxpay.TransferApplyResponse{
		OutDetailNo: returnDTO.OutDetailNo,
	}, nil
}

// 查询转账单详情 GET /api/v1/wxpay/transfer
func (s *Server) TransferQuery(ctx context.Context, request *wxpay.TransferQueryRequest) (*wxpay.TransferQueryResponse, error) {
	detail, err := service.NewTransferService(s.d, s.redis).TransferDetailQueryByBizNo(ctx, request.BizNo)
	if err != nil {
		return nil, err
	}
	return &wxpay.TransferQueryResponse{
		AppId:      detail.AppID,
		MchId:      detail.MchID,
		Amount:     detail.Amount,
		Remark:     detail.Remark,
		OpenId:     detail.OpenID,
		UserName:   detail.UserName,
		UserIdCard: detail.UserIDCard,
		BizNo:      detail.BizDetailNo,
		OutDetail:  detail.OutDetailNo,
		Status:     detail.Status,
		Reason:     detail.FailReason,
	}, nil
}
